from sklearn.datasets import load_wine
from sklearn import tree
from sklearn.model_selection import train_test_split

wine = load_wine()

x_train, x_test, y_train, y_test = train_test_split(wine.data, wine.target, test_size=0.3, random_state=22)
clf = tree.DecisionTreeClassifier(criterion='entropy', random_state=22, splitter='random',
                                  # ,max_depth=10, min_samples_leaf=3, min_samples_split=3
                                  )
# splitter表示特征的重要性都一样 默认best表示按重要度选特征 特征较多时 random可以防过拟合(过度关注某些特征)
clf.fit(x_train, y_train)
# acc = clf.score(x_test, y_test)
# print(acc)

import graphviz
feature_name = ['酒精','苹果酸','灰','灰的碱性','镁','总酚','类黄酮','非黄烷类酚类',
                '花青素','颜色强度','色调','od280/od315稀释葡萄酒','脯氨酸']
ex_grap = tree.export_graphviz(clf, feature_names=feature_name,
                                  class_names=["琴酒","雪莉","贝尔摩德"],
                                  filled=True, rounded=True)
grap = graphviz.Source(ex_grap.replace('helvetica', 'fangsong'))
grap.view()